package kr.saeildamil.androidStudy_second.fragment.cp;

import kr.saeildamil.androidStudy_second.R;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class EnglishWord extends Activity {
	WordDBHelper mHelper;
	EditText mText;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch26_englishword);
		mHelper = new WordDBHelper(this);
		mText = (EditText) findViewById(R.id.ch26_englishword_edittext);
	}

	public void mOnClick(View v) {
		SQLiteDatabase db;
		ContentValues row;

		switch (v.getId()) {
		case R.id.ch26_englishword_insert:
			db = mHelper.getWritableDatabase();
			// dbhelper 에서 지원하는 메소드를 이용
			row = new ContentValues();
			row.put("eng", "boy");
			row.put("han", "머시마");

			db.insert("dic", null, row);

			// SQL문으로 삽입
			String sql = "insert into dic values(null, 'girl', '가시나');";
			db.execSQL(sql);

			mText.setText("Insert Success");
			break;

		case R.id.ch26_englishword_select:
			db = mHelper.getWritableDatabase();
			Cursor cursor;
			cursor = db.query("dic", new String[] { "_id", "eng", "han" },
					null, null, null, null, null);
			// cursor = db.rawQuery("select _id, eng, han from dic", null);

			StringBuilder sb = new StringBuilder();

			while (cursor.moveToNext()) {
				sb.append(cursor.getInt(0) + " : ");
				sb.append(cursor.getString(1) + " = ");
				sb.append(cursor.getString(2) + "\n");
			}
			if (sb.length() == 0) {
				mText.setText("Empty Set");
			} else {
				mText.setText(sb.toString());
			}
			cursor.close();
			mHelper.close();
			break;

		case R.id.ch26_englishword_delete:
			db = mHelper.getWritableDatabase();
			db.delete("dic", null, null);
			// db.execSQL("delete from dic");

			mText.setText("Delete Success");
			mHelper.close();
			break;

		case R.id.ch26_englishword_update:
			db = mHelper.getWritableDatabase();

			row = new ContentValues();
			row.put("han", "소년");
			db.update("dic", row, "eng='boy'", null);
			sql = "update dic set han = '소녀' where eng = 'girl';";
			db.execSQL(sql);
			mHelper.close();
			mText.setText("Update Success");
			break;

		}
	}
}
